<?php
declare (strict_types=1);

namespace app\service;

use app\controller\admin\Label;
use app\model\TLabel;
use app\model\User;
use app\model\UserDetails;
use app\model\UserFenggeScore;
use app\traits\ServiceTrait;
use think\facade\Db;
use app\model\ReportDic;
use app\model\FenggeParam;

/**
 * 客户
 * Class ClientService
 * @package app\service
 * @author  2066362155@qq.com
 */
class ClientService
{
    
    use ServiceTrait;

    /**
     * @param $field
     * @return array
     */
    public static function userList($param)
    {
        $title = $param['title'];
        if (!empty($title)) {
            $userList = User::where([['nick_name', 'like', '%' . $param['title'] . '%']])
                ->field(['user_id', 'nick_name', 'head_image', 'sida_num'])
                ->order('create_time', 'desc')
                ->paginate(['page' => 1, 'list_rows' => $param['pageSize']])
                ->toArray();
        } else {
            $userList = User::field(['user_id', 'nick_name', 'head_image', 'sida_num'])
                ->order('create_time', 'desc')
                ->paginate(['page' => $param['page'], 'list_rows' => $param['pageSize']])
                ->toArray();
        }
        $userData = [];
        if (!empty($userList['data'])) {
            $userData = $userList['data'];
        }
        if (empty($userData)) {
            return $userList;
        }
        foreach ($userData as &$u) {
            $detail = UserDetails::where(['user_id' => $u['user_id']])->field(['tixing', 'shengao', 'tizhong', 'nianling'])->find();
            $r = static::getStyle($u['user_id']);
            $tixing = TLabel::where(['id' => $detail['tixing']])->column('code_name');
            $u['tixing'] = $tixing[0] ?? '';
            $u['nianling'] = $detail['nianling'] ?? '';
            $u['tizhong'] = $detail['tizhong'] ?? '';
            $u['shengao'] = $detail['shengao'] ?? '';
            $u['fengge'] = $r;
        }
        $userList['data'] = $userData;
        return $userList;
    }

    protected static function getStyle($userId)
    {
        $fields = ['shaonv', 'youya', 'langman', 'ziran', 'gudian', 'xiju', 'shaonian', 'romantic', 'ruizhi', 'yiyuziran', 'qianwei'];
        $userFenggeScore = UserFenggeScore::where(['user_id' => $userId])->field($fields)->find();
        if (empty($userFenggeScore)) {
            return '';
        }
        $labels = config('goods.labels');
        $d = [];
        foreach ($fields as $field) {
            $d[$userFenggeScore->$field] = $labels[$field];
        }
        krsort($d);
        $labelId = current($d);
        
        if (empty($labelId)) {
            return '';
        }

        $fenggeData = TLabel::where(['id' => $labelId])->column('code_name');

        $fengge = $fenggeData[0] ?? '';
        //echo '<pre>';var_dump($fengge);

        return $fengge;
    }
}

